﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace RRD2.DAL
{
    public class Logger
    {
        Database database = new Database();

        public static void WriteException(Exception ex)
        {
            try
            {
                string fileName = string.Format("{0}\\{1}"
                    , ConfigurationManager.AppSettings["DirectoryExceptionLogFile"].ToString()
                    , "exceptionLog.log");

                using (var sw = new StreamWriter(fileName, true))
                {
                    string value = string.Format("{0};{1};{2};{3};{4};{5}"
                        , DateTime.Now.ToString()
                        , ex.Message
                        , "Exception"
                        , ex.StackTrace
                        , Environment.UserName
                        , Environment.MachineName
                        );
                    sw.WriteLine(value);
                }
            }
            catch (Exception)
            {
                //WriteException(ex);
            }
        }
        
        public void WriteLog(RRD2.Model.Statement _statement, string _barcode)
        {
            try
            {
                Model.Log log = new Model.Log();
                log.Statement = _statement;
                log.DateTime = DateTime.Now;
                log.Barcode = _barcode;
                log.UserId = Environment.UserName;
                log.WorkstationId = Environment.MachineName;
                switch(ConfigurationManager.AppSettings["WriteLogType"])
                {
                    case "DB":
                        Log2DB(log);
                        break;
                    case "BOTH":
                        Log2DB(log);
                        Log2File(log);
                        break;
                    case "MAIL":
                        Controler.Messanger.SendMail(_statement);
                        break;
                    case "FILE":
                        Log2File(log);
                        break;                
                }
            }
            catch (Exception ex)
            {
                WriteException(ex);
            }
        }

        private void Log2File(Model.Log _log)
        {
            try
            {
                string fileName = string.Format("{0}\\{1}.csv"
                    , ConfigurationManager.AppSettings["DirectoryLogFile"].ToString()
                    , DateTime.Now.ToShortDateString());

                using (var sw = new StreamWriter(fileName,true))
                {
                    string value = string.Format("{0};{1};{2};{3};{4};{5}"
                        , _log.DateTime.ToString()                        
                        ,_log.Statement.Value
                        ,_log.Statement.Type
                        ,_log.Barcode
                        ,_log.UserId
                        ,_log.WorkstationId
                        );
                    sw.WriteLine(value);
                }
            }
            catch (Exception ex)
            {
                WriteException(ex);
            }
        }

        private void Log2DB(Model.Log _log)
        {
            try
            {
                database.WriteLog(_log);
            }
            catch (Exception ex)
            {
                WriteException(ex);
            }

        }
    }
}
